package cn.doitedu.day07

import org.apache.spark.sql.types.{DataType, DataTypes}
import org.apache.spark.sql.{DataFrame, SparkSession}

//DSL 领域特定语言
object T25_DSLDemo3 {

  def main(args: Array[String]): Unit = {

    val spark = SparkSession.builder().appName(this.getClass.getSimpleName)
      .master("local[*]")
      .getOrCreate()

    //创建DataFrame
    //创建DataFrame
    val df = spark.read
      .csv("data/game.txt")
      .toDF("uid", "gid", "zid", "money", "os")

    import spark.implicits._
    import org.apache.spark.sql.functions._
    //不用注册视图，直接调用dataFrame上的方法
    val df2: DataFrame = df.select($"gid", $"zid", $"money".cast(DataTypes.DoubleType))

//    df2.groupBy($"gid", $"zid")
//      .sum("money").withColumnRenamed("sum(money)", "sum_money")
//      .show()

    df2.groupBy($"gid", $"zid")
      .agg(
        count("*") as "counts",
        sum("money").as("sum_money"),
        avg("money").as("avg_money"),
      )
      .show()
  }
}
